import 'package:flutter/material.dart';

class InheritedWidgetTestPage extends StatefulWidget {
  const InheritedWidgetTestPage({Key? key}) : super(key: key);

  @override
  _InheritedWidgetTestPageState createState() => _InheritedWidgetTestPageState();
}

class _InheritedWidgetTestPageState extends State<InheritedWidgetTestPage> {
  late InheritedTestModel inheritedTestModel;

  @override
  void initState() {
    inheritedTestModel = InheritedTestModel(100);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return InheritedContext(
        inheritedTestModel,
        _incrementCount,
        _reduceCount,
        Scaffold(
          appBar: AppBar(
            title: const Text("InheritedWidgetTestPage"),
          ),
          body: Column(
            children: const [
              SizedBox(height: 20),
              TestWidgetA(),
              SizedBox(height: 20),
              TestWidgetB(),
              SizedBox(height: 20),
              TestWidgetC(),
            ],
          ),
        ));
  }

  _incrementCount() {
    setState(() {
      inheritedTestModel = InheritedTestModel(inheritedTestModel.count + 1);
    });
  }

  _reduceCount() {
    setState(() {
      inheritedTestModel = InheritedTestModel(inheritedTestModel.count - 1);
    });
  }
}

class TestWidgetA extends StatelessWidget {
  const TestWidgetA({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);

    // final inheritedTestModel = inheritedContext.inheritedModel;
    //
    // print('TestWidgetA 中count的值:  ${inheritedTestModel.count}');
    debugPrint("build +");

    return Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
      child: MaterialButton(
          textColor: Colors.black, child: const Text('+'), onPressed: inheritedContext?.increment),
    );
  }
}

class TestWidgetB extends StatelessWidget {
  const TestWidgetB({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);

    final inheritedTestModel = inheritedContext?.inheritedModel;

    debugPrint('TestWidgetB 中count的值:  ${inheritedTestModel?.count ?? 0}');

    return Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
      child: Text(
        '当前count:${inheritedTestModel?.count ?? 0}',
        style: const TextStyle(fontSize: 20.0),
      ),
    );
  }
}

class TestWidgetC extends StatelessWidget {
  const TestWidgetC({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final inheritedContext = InheritedContext.of(context);

    // final inheritedTestModel = inheritedContext.inheritedModel;
    //
    // print('TestWidgetC 中count的值:  ${inheritedTestModel.count}');
    debugPrint("build -");
    return Padding(
      padding: const EdgeInsets.only(left: 10.0, top: 10.0, right: 10.0),
      child: MaterialButton(
          textColor: Colors.black, child: const Text('-'), onPressed: inheritedContext?.reduce),
    );
  }
}

class InheritedTestModel {
  final int count;

  InheritedTestModel(this.count);
}

class InheritedContext extends InheritedWidget {
  //数据
  final InheritedTestModel inheritedModel;

  //点击+号的方法
  final Function() increment;

  //点击-号的方法
  final Function() reduce;

  const InheritedContext(this.inheritedModel, this.increment, this.reduce, Widget child, {Key? key})
      : super(key: key, child: child);

  static InheritedContext? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<InheritedContext>();
  }

  @override
  bool updateShouldNotify(InheritedContext oldWidget) {
    return inheritedModel != oldWidget.inheritedModel;
  }
}
